/*
 * Copyright (c) 2009 Roman Savelyev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef __APPHOOKMODEL_H__
#define __APPHOOKMODEL_H__

#include <e32base.h>
#include "HostLink.h"

class CWaitHostSignal;
class CReadyForE32Image;
class CReceiveE32ImageHeader;
class CReceiveImage;
class CReceiveMapFileData;
class CSandboxServer;
class CSandbox;

enum TSandboxingStage
    {
    ENotSandboxing,
    EWaitE32ImageSize,
    EReadyForE32Image,
    EReceiveE32ImageHeader,
    EReceiveImage,
    EReceiveMapData,
    ERunSandboxServer
    };

class MAppHookModelObserver
    {
public:
    virtual void SandboxingComplete(TSandboxingStage aStage, TInt aError) = 0;
    };

class CAppHookModel : public CBase, public MHostLinkObserver
    {
public:
    CAppHookModel(MAppHookModelObserver& aObserver);
    ~CAppHookModel();
    void ConnectL();
    void Disconnect();
    TBool Connected() const;
    CHostLink& HostLink();
    const CHostLink& HostLink() const;
    void CreateSandboxL(TUint32 aImageSize);
    CSandbox& Sandbox();
    const CSandbox& Sandbox() const;
public:
    void WaitE32ImageSizeComplete(TInt aError);
    void ReadyForE32ImageComplete(TInt aError);
    void ReceiveE32ImageHeaderComplete(TInt aError);
    void ReceiveImageComplete(TInt aError);
    void ReceiveMapFileDataComplete(TInt aError);
    void SandboxServerComplete(TInt aError);
private:
	void LinkStatusChanged();
	void HostError();
	void DeviceErrorComplete(TInt aError);
private:
	void CompleteSandboxing(TInt aError);
private:
    MAppHookModelObserver& iObserver;
    CHostLink* iHostLink;
    CSandbox* iSandbox;
    CBase* iCurrentState;
    TSandboxingStage iStateId;
    TInt iSandboxingError;
    };

#endif /*__APPHOOKMODEL_H__*/
